<?php
declare (strict_types = 1);

namespace app\product\middleware;

use app\auth\service\AdminService;

class Check
{
    public function __construct(AdminService $adminService){
        $this->adminService = $adminService;
    }
    /**
     * 检查登录
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        $user_info = $this->isLogin();
        if(!is_array($user_info)) return $user_info;
        // 中间件向控制器传参
        $request->user_id = $user_info['user_id'];
        $request->admin_id = $user_info['admin_id'];
        $request->role_range = $user_info['role_range'];
        return $next($request);
    }

    public function isLogin(){
        $token = request()->header('token')??'';
        return $this->adminService->checkLogin($token);
    }
}
